<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <!-- 添加绑定点击事件打开弹框 -->
        <el-button size="mini" type="primary" @click="$refs.addRole.showDialog=true">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column prop="name" align="center" width="200" label="角色">
          <template slot-scope="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRole.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" align="center" width="200" label="启用">
          <template slot-scope="{ row }">
            <el-switch v-if="row.isEdit" v-model="row.editRole.state" :active-value="1" :inactive-value="0" />
            <span v-else>  {{ row.state === 1 ? "启用" : "禁用" }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述">
          <template slot-scope="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRole.description" type="textarea" size="mini" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="{ row }">
            <template v-if="row.isEdit">
              <el-button type="primary" size="mini" @click="editRow(row)">确定</el-button>
              <el-button size="mini" @click="row.isEdit = false">取消</el-button>
            </template>
            <template v-else>
              <el-button size="mini" type="text" @click="openAuth(row.id)">设置权限</el-button>
              <el-button size="mini" type="text" @click="row.isEdit = true">编辑</el-button>
              <el-popconfirm
                title="确定删除吗"
                confirm-button-text="确定"
                cancel-button-text="取消"
                @onConfirm="btnCancel(row.id)"
              >
                <el-button slot="reference" size="small" type="text">删除</el-button>
              </el-popconfirm>

            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          :current-page.sync="queryData.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="queryData.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
          @size-change="sizeChange"
          @current-change="currentChange"
        >
          :pager-count="7">
        </el-pagination>
      </el-row>
      <!-- 新增 -->
      <add-role ref="addRole" @updateList="initDate" />
      <!-- 权限弹框 -->
      <set-auth ref="setAuth" />
    </div>
  </div>
</template>
<script>
import { deleteRoleApi, editRoleApi, getRoleApi, infoRoleApi } from '@/api/role'
import addRole from './components/addRole.vue'
import setAuth from './components/setAuth.vue'
export default {
  name: 'Role',
  components: { addRole, setAuth },
  data() {
    return {
      // 表格变量
      list: [],
      // 查询变量
      queryData: {
        page: 1,
        pagesise: 10

      },
      // 总数
      total: 0
    }
  },
  created() {
    this.initDate()
  },
  methods: {
    // 获取列表数据
    async initDate() {
      // 调用获取列表Api
      const res = await getRoleApi(this.queryData)
      // console.log(res)
      // 赋值变量-列表和总数
      this.list = res.rows
      this.list.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRole', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
      this.total = res.total
    },
    sizeChange(val) {
      this.queryData.pagesize = val
      this.initDate()
    },
    currentChange(val) {
      this.queryData.page = val
      this.initDate()
    },
    // 删除
    async btnCancel(id) {
      // 发送请求
      await deleteRoleApi(id)
      // 提示成功
      this.$message.success('删除成功')
      // 处理边缘问题
      if (this.queryData.page > 1 && this.list.length === 1) {
        this.queryData.page--
      }
      // 重新拉数据
      this.initDate()
    },
    // 修改确定
    async editRow(row) {
      // 校验
      if (!row.editRole.name || !row.editRole.description) {
        return this.$$message.warning('请输入名称和描述')
      }
      // 发送请求
      await editRoleApi({ ...row.editRole, id: row.id })
      // 成功提示
      this.$message.success('修改成功')
      // 列表更新，关闭弹框
      Object.assign(row, { ...row.editRole, isEdit: false })
    },
    // 打开关联权限弹框
    async openAuth(id) {
      // 根据ID查询角色信息，将角色的权限信息赋值给checkAuth
      const res = await infoRoleApi(id)
      this.$refs.setAuth.roleId = id
      this.$refs.setAuth.checkAuth = res.permIds
      this.$refs.setAuth.closeBox = true
    }
  }
}
</script>
<style scoped>
.role-operate {
  padding: 10px;
}
</style>
